package com.xinbochuang.machine.admin.service.impl.scrap;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinbochuang.machine.admin.domain.macRoom.Equipment;
import com.xinbochuang.machine.admin.domain.scrap.Scrap;
import com.xinbochuang.machine.admin.domain.system.SysUser;
import com.xinbochuang.machine.admin.mapper.scrap.ScrapMapper;
import com.xinbochuang.machine.admin.service.macRoom.IEquipmentService;
import com.xinbochuang.machine.admin.service.scrap.IScrapService;
import com.xinbochuang.machine.common.annotation.DataScope;
import com.xinbochuang.machine.common.enums.SimpleCheckStatus;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.common.utils.ServletUtils;
import com.xinbochuang.machine.framework.web.service.TokenService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author 黄晓鹏
 * @date 2020-10-25
 */
@Service
public class ScrapServiceImpl extends ServiceImpl<ScrapMapper, Scrap> implements IScrapService {

    @Resource
    private ScrapMapper scrapMapper;

    @Resource
    private TokenService tokenService;

    @Resource
    private IEquipmentService equipmentService;

    /**
     * 下电设备分页列表
     *
     * @param equipment 查询条件
     * @return 结果
     */
    @Override
    public IPage<Equipment> selectEquList(Equipment equipment) {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        equipment.setDeptId(user.getDeptId());
        return scrapMapper.getEquList(new Page<>(equipment.getPageNum(), equipment.getPageSize()), equipment);
    }

    /**
     * 利旧/报废分页列表
     *
     * @param scrap 查询条件
     * @return 结果
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public IPage<Scrap> selectScarpList(Scrap scrap) {
        return scrapMapper.getList(new Page<>(scrap.getPageNum(), scrap.getPageSize()), scrap);
    }

    /**
     * 根据设备id查询机房/机柜
     *
     * @param equId 设备id
     * @return 结果
     */
    @Override
    public String selectPlace(String equId) {
        return scrapMapper.selectPlace(equId);
    }

    /**
     * 插入申请利旧/报废
     *
     * @param scrap 实体类
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(Scrap scrap) {
        scrap.setCreateBy(SecurityUtils.getUsername());
        scrap.setCreateTime(DateUtil.now());
        save(scrap);
        Equipment equ = new Equipment();
        equ.setUuid(scrap.getEquId());
        equ.setScrapStatus("1");
        equipmentService.updateById(equ);
        return true;
    }

    /**
     * 审批利旧/报废申请
     *
     * @param scrap 参数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeStatus(Scrap scrap) {
        scrap.setUpdateBy(SecurityUtils.getUsername());
        scrap.setUpdateBy(DateUtil.now());
        updateById(scrap);
        Scrap info = getById(scrap.getId());
        Equipment equ = new Equipment();
        equ.setUuid(info.getEquId());
        if (scrap.getStatus().equals(SimpleCheckStatus.NOMAIL.getCode())) {
            //审批通过
            if ("0".equals(info.getType())) {
                equ.setScrapStatus("2");
            } else {
                equ.setScrapStatus("3");
            }
        } else if (scrap.getStatus().equals(SimpleCheckStatus.NOPASS.getCode())) {
            //审批不通过
            equ.setScrapStatus("0");
        }
        equipmentService.updateById(equ);
        return true;
    }
}
